package com.isoft.game.sys.role.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.isoft.game.common.component.PageUtil;
import com.isoft.game.common.model.PersistModel;
import com.isoft.game.common.service.BaseService;
import com.isoft.game.common.util.FamilyLogger;
import com.isoft.game.common.util.UserUtils;
import com.isoft.game.sys.role.dao.RoleRepository;
import com.isoft.game.sys.role.entity.Role;
import com.isoft.game.sys.role.service.RoleService;
import com.isoft.game.sys.user.entity.User;

@Service
@Transactional(readOnly = true,propagation = Propagation.NOT_SUPPORTED)
public class RoleServiceImpl extends BaseService implements RoleService {

	private static final long serialVersionUID = 1L;
	
	@Autowired
    private RoleRepository roleRepository;
	
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
    @Override
    public PersistModel persistRole(Role role,User user) {
    	role.setCommonBusinessId();
    	UserUtils.setCurrentPersistOperation(role,user);
        int line = roleRepository.insertSelective(role);
        FamilyLogger.sysInfo("当前登陆人"+ user.getBusinessId(),"当前登陆人" + user.getUsername() +"新增了ID为"+role.getBusinessId()+"的角色");
        return new PersistModel(line);
    }

    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
    @Override
    public PersistModel mergeRole(Role role,User user) {
    	UserUtils.setCurrentMergeOperation(role, user);
        int line = roleRepository.updateByPrimaryKeySelective(role);
        FamilyLogger.sysInfo("当前登陆人"+ user.getBusinessId(),"当前登陆人" + user.getUsername() +"修改了ID为"+role.getBusinessId()+"的角色");
        return new PersistModel(line);
    }

    @Override
    public List<Role> queryRolesByPagination(PageUtil<Role> paginationUtility, Role role) {
        return roleRepository.queryRoles(role);
    }
    

	@Override
	public Role queryRoleById(String id) {
	       return roleRepository.selectByPrimaryKey(id);
	}
}
